package cn.hotel.dao;

import cn.hotel.utlis.Crud;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class AdministratorDao extends Crud implements Administrator {

    //查询管理员
    @Override
    public List<cn.hotel.pojo.Administrator> administratorList(String administrator) {
        String sql = "SELECT * FROM ADMINISTRATOR ";
        StringBuffer sbf = new StringBuffer(sql);
        List<Object> data = new ArrayList<>();
        if(administrator != null){
            sbf.append(" WHERE ADMINISTRATORNUMBER = ?");
            data.add(administrator);
        }
        ResultSet resultSet = getResultSet(sbf.toString(),data);
        List<cn.hotel.pojo.Administrator> administratorList = new ArrayList<>();
        cn.hotel.pojo.Administrator administrator1 = null;

        try {
            while (resultSet.next()) {
                administrator1 = new cn.hotel.pojo.Administrator();
                administrator1.setId(resultSet.getLong(1));
                administrator1.setAdministratorNumber(resultSet.getString(2));
                administrator1.setPassword(resultSet.getString(3));
                administrator1.setMemberJurisdiction(resultSet.getInt(4));
                administrator1.setDoBusinessJurisdiction(resultSet.getInt(5));
                administrator1.setGoodsJurisdiction(resultSet.getInt(6));
                administrator1.setManageJurisdiction(resultSet.getInt(7));
                administrator1.setLogJurisdiction(resultSet.getInt(8));
                administrator1.setFinanceJurisdiction(resultSet.getInt(9));
                administratorList.add(administrator1);
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        } finally {
            this.setClear(resultSet);
        }
        return administratorList;
    }

    //添加管理员
    @Override
    public int addAdministrator(cn.hotel.pojo.Administrator administrator) {
        String sql = "INSERT INTO `hotel`.`administrator` (`administratorNumber`, `password`, `memberJurisdiction`, `doBusinessJurisdiction`, " +
                "`goodsJurisdiction`, `manageJurisdiction`, `logJurisdiction`, `financeJurisdiction`) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
        List<Object> data = new ArrayList<>();
        data.add(administrator.getAdministratorNumber());
        data.add(administrator.getPassword());
        data.add(administrator.getMemberJurisdiction());
        data.add(administrator.getDoBusinessJurisdiction());
        data.add(administrator.getGoodsJurisdiction());
        data.add(administrator.getManageJurisdiction());
        data.add(administrator.getLogJurisdiction());
        data.add(administrator.getFinanceJurisdiction());
        return getInt(sql,data);
    }

    //删除管理员
    @Override
    public int deleteAdministrator(String name) {
        String sql = "DELETE FROM ADMINISTRATOR WHERE ADMINISTRATORNUMBER = ?";
        List<Object> data = new ArrayList<>();
        data.add(name);
        return getInt(sql,data);
    }

    //修改管理员权限
    @Override
    public int change(cn.hotel.pojo.Administrator administrator) {
        String sql = "UPDATE `ADMINISTRATOR` SET`MEMBERJURISDICTION`=?, `DOBUSINESSJURISDICTION`=?, `GOODSJURISDICTION`=?, `MANAGEJURISDICTION`=?, `LOGJURISDICTION`=?, `FINANCEJURISDICTION`=? WHERE ADMINISTRATORNUMBER = ?";
        List<Object> data = new ArrayList<>();
        data.add(administrator.getMemberJurisdiction());
        data.add(administrator.getDoBusinessJurisdiction());
        data.add(administrator.getGoodsJurisdiction());
        data.add(administrator.getManageJurisdiction());

        data.add(administrator.getLogJurisdiction());
        data.add(administrator.getFinanceJurisdiction());
        data.add(administrator.getAdministratorNumber());
        return getInt(sql,data);
    }
}
